package com.soft.link.controller;

import com.alibaba.fastjson.JSON;
import com.soft.link.impl.RedisService;
import com.soft.link.model.TsUser;
import com.soft.link.service.TsUserService;
import com.soft.link.utils.MsgEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/portal")
public class PortalLoginController extends SpringBootServletInitializer {
    public Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired(required = false)
    TsUserService tsUserService;

    @Autowired(required = false)
    HttpServletRequest request ;

    @Autowired(required = false)
    private RedisService redisService;
    /**
    * @methodname  登录入口，进入登录界面
    * @Description {填写方法注释说明}
    * @author 黄学乾
    * @create 2018/3/22/022 23:21
    * @throws {如果有异常说明请填写}
    * @return
    */
    @RequestMapping("/login")
    public String portalLogin(HttpServletRequest request, ModelMap model){
        return "login/login" ;
    }

    /**
    * @methodname
    * @Description {填写方法注释说明}
    * @author 黄学乾
    * @create 2018/3/23/023 19:32
    * @throws {如果有异常说明请填写}
    * @return {status:success;msgCode;msgContent}
    */
    @RequestMapping("/doLogin")
    @ResponseBody
    public Map doLogin(String loginSUser, String passwordSUser) {
        Map<String, Object> resCode = new HashMap<>();
        try {
            TsUser tsUser = tsUserService.findTsUser(loginSUser, passwordSUser);
            String sessionId = request.getSession().getId();
            //判断当前登录人是否锁定
            if (tsUser.getIslock() == 0) {
                redisService.set(sessionId, JSON.toJSONString(tsUser));
                resCode.put("status",true);
                resCode.put("msgCode",MsgEnum.Login_Msg_Success.getCode());
                resCode.put("msgContent",MsgEnum.Login_Msg_Success.getDesc());
            }else{
                resCode.put("status",false);
                resCode.put("msgCode",MsgEnum.Login_Msg_False.getCode());
                resCode.put("msgContent",MsgEnum.Login_Msg_False.getDesc());
            }
            logger.info("-------------->redis:" + redisService.get(sessionId));
        } catch (Exception e) {
            resCode.put("status",false);
            resCode.put("msgCode",MsgEnum.Login_Msg_False.getCode());
            resCode.put("msgContent",MsgEnum.Login_Msg_False.getDesc());
        }
        return resCode;
    }


    /**
    * @methodname  退出登录
    * @Description 退出登录，记录日志并销毁所有会话
    * @author 黄学乾
    * @create 2018/3/22/022 23:18
    * @param  * @param
    * @throws {如果有异常说明请填写}
    * @return
    */
    @RequestMapping("/exitLogin")
    public ModelAndView exitLogin(HttpServletRequest request){
        /**
         * add 清除当前事件 的登录方法 ，
         */
        return new ModelAndView("login/login") ;
    }

}
